<?php

namespace IDock\Insurance\DAJIA\Response;

use IDock\Http\Response;
use IDock\Insurance\DAJIA\Response\AllDAJIAResponse;
use IDock\Printer\PolicyPaymentPrinter;
use IDock\Printer\ProposalStatusPrinter;

class OrderQueryResponse extends AllDAJIAResponse
{

    public function printerInit(): Response
    {
        $proposalStatusPrinter = new ProposalStatusPrinter();

        $result = $this->get('body', [])['result'] ?? [];
        $ciList = [];//交强险列表
        $biList = [];//商业险列表
        foreach ($result as $val) {
            if ($val['prodNo'] == '0320') {
                $ciList[] = $val;
            } else {
                $biList[] = $val;
            }

            if ($val['prodNo'] == '0320') {
                //交强险
                $proposalStatusPrinter->ciInquiryNo = $val['appNo'];
                $proposalStatusPrinter->ciPolicyNo = $val['plyNo'];
                $proposalStatusPrinter->ciStartData = $val['effectiveDate'];
                $proposalStatusPrinter->ciEndData = $val['expireDate'];
            } else {
                $proposalStatusPrinter->biInquiryNo = $val['appNo'];
                $proposalStatusPrinter->biPolicyNo = $val['plyNo'];
                $proposalStatusPrinter->biStartData = $val['effectiveDate'];
                $proposalStatusPrinter->biEndData = $val['expireDate'];
            }
        }
        $error = [];
        $ciStatus = $biStatus = '';
        if (!empty($ciList)) {
            $ciRes = $this->getAppStatus($ciList);
            $ciStatus = $ciRes['code'];
        }
        if (!empty($biList)) {
            $biRes = $this->getAppStatus($biList);
            $biStatus = $biRes['code'];
        }
        if (!empty($ciStatus) && !empty($biStatus)) {
            //交商同保
            $status = 'U4';
            if ($ciStatus == 'U2' or $biStatus == 'U2') {
                $status = 'U2';
            } elseif ($ciStatus == 'U3' or $biStatus == 'U3') {
                $status = 'U3';
            } elseif ($ciStatus == 'U4' or $biStatus == 'U4') {
                $status = 'U4';
            }elseif($ciStatus == 'U1' or $biStatus == 'U1') {
                $status = 'U1';
            } elseif ($ciStatus == 'U5' or $biStatus == 'U5') {
                $status = 'U5';
            }
        } else {
            $status = $biStatus ?: $ciStatus;
        }
        //错误提示
        if(in_array($ciRes['code'] ?? '',['U2','U4'] )){
            $error[] = $ciRes['message'];
        }
        if(in_array($biRes['code'] ?? '',['U2','U4'] )){
            $error[] = $biRes['message'];
        }
        if(!empty($error)){
            $proposalStatusPrinter->messageTip = implode("\r\n",$error);
        }

        $proposalStatusPrinter->statusCode = $status;
        $this->printer = $proposalStatusPrinter;
        return $this;
    }

    private function getAppStatus($rowList)
    {
        $code = '';
        array_multisort(array_column($rowList, 'oprTime'), SORT_ASC, $rowList);
        $lastRow = array_pop($rowList);
        $appStatus = $lastRow['appStatus'];
        $res = ['code'=>'','message'=>$lastRow['oper']];
        if ($appStatus == '1') {
            $code = 'U2';//退回修改
        } else if ($appStatus == '2') {
            $code = 'U3';//人工核保
        } else if ($appStatus == '3' || $appStatus == '8') {
            $code = 'U4';//核保失败
        } elseif ($appStatus == '4' && !in_array($lastRow['notifyType'],['2009','2010'])) {
            $code = 'U1';//核保通过
        } elseif ($appStatus == '4' && in_array($lastRow['notifyType'],['2009','2010'])) {
            $code = 'U5';//已出保单
        }
        if($lastRow['notifyType'] == '2003'){
            $res['message'] = ' 出单员撤回';
        }
        $res['code'] = $code;
        return $res;
    }
}
